package com.example.curvedemo.ui.main

import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import androidx.activity.ComponentActivity
import androidx.activity.enableEdgeToEdge
import com.example.curvedemo.R
import com.example.curvedemo.widget.CurveNewData
import com.example.curvedemo.widget.CurveNewTipView
import kotlin.random.Random

class MainActivity : ComponentActivity() {
    var xList: MutableList<CurveNewData.XData> = mutableListOf()
    var lineList: MutableList<CurveNewData.LineData> = mutableListOf()
    var yOnedatas: MutableList<CurveNewData.LineData.ItemData> = mutableListOf()
    var yTwodatas: MutableList<CurveNewData.LineData.ItemData> = mutableListOf()
    var yThreedatas: MutableList<CurveNewData.LineData.ItemData> = mutableListOf()
    var yFourdatas: MutableList<CurveNewData.LineData.ItemData> = mutableListOf()
    var curveNewView: CurveNewTipView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //Android 15以上被强制要求所有应用适配，若未适配，可能导致用户体验下降
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        initCtrl()
    }

    /**
     * 初始化操作
     */
    private fun initCtrl() {
        curveNewView = findViewById(R.id.curve)
        curveNewView?.setOnClickListener(object: OnClickListener {
            override fun onClick(v: View?) {

            }
        })
        setData()
    }

    /**
     * 设置曲线图数据
     */
    fun setData() {
        xList.clear()
        lineList.clear()
        yOnedatas.clear()
        yTwodatas.clear()
        yThreedatas.clear()
        yFourdatas.clear()
        for (i in 1..30) {
            if (i < 10) {
                xList.add(CurveNewData.XData("08-0" + i, "08-0" + i))
            } else {
                xList.add(CurveNewData.XData("08-" + i, "08-" + i))
            }
            val randomOne = Random.nextLong(9999999)
            yOnedatas.add(CurveNewData.LineData.ItemData(randomOne, convertToWan(randomOne)))
            val randomTwo = Random.nextLong(9999999)
            yTwodatas.add(CurveNewData.LineData.ItemData(randomTwo, convertToWan(randomTwo)))
            val randomthree = Random.nextLong(9999999)
            yThreedatas.add(
                CurveNewData.LineData.ItemData(
                    randomthree,
                    convertToWan(randomthree)
                )
            )
            val randomFour = Random.nextLong(9999999)
            yFourdatas.add(
                CurveNewData.LineData.ItemData(
                    randomFour,
                    convertToWan(randomFour)
                )
            )
        }
        lineList.clear()
        lineList.add(CurveNewData.LineData("点赞", yOnedatas))
        lineList.add(CurveNewData.LineData("评论", yTwodatas))
        lineList.add(CurveNewData.LineData("分享", yThreedatas))
        lineList.add(CurveNewData.LineData("收藏", yFourdatas))
        curveNewView?.setData(xList, lineList, 3)
    }

    /**
     * 单位换算
     */
    fun convertToWan(number: Long): String {
        val wan = number / 10000.0 // 换算成万
        return String.format("%.2f万", wan) // 格式化为两位小数的字符串
    }
}

